package com.itouchtv.yang.library.element;

import com.itouchtv.yang.TestCase.BaseTest;
import com.itouchtv.yang.core.tools.AnalysisXml;
import org.dom4j.Document;
import org.dom4j.Node;
import org.openqa.selenium.By;
import org.testng.annotations.Parameters;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ElementBy {
    private ElementBy(){
    }
    public static Map<String,By> getElementBy(){
        String classPath =BaseTest.class.getResource("/").toString();
        Map<String,By> elementBys = new HashMap<>();
        AnalysisXml analysisXml = new AnalysisXml();
        Document document = analysisXml.getDocument(classPath+ "library/library_gx.xml");
        List<Node> elements = document.selectNodes("pages/page/element");
        for (Node element:elements){
            String type = element.valueOf("@type").toUpperCase();
            String value = element.valueOf("@value");
            String name = element.getText();
            switch (type){
                case "ID":
                    elementBys.put(name,new By.ById(value));
                    break;
                case "NAME":
                    elementBys.put(name,new By.ByName(value));
                    break;
                case "CLASS":
                    elementBys.put(name,new By.ByClassName(value));
                    break;
                case "TAG":
                    elementBys.put(name,new By.ByTagName(value));
                    break;
                case "XPATH":
                    elementBys.put(name,new By.ByXPath(value));
                    break;
                case "CSS":
                    elementBys.put(name,new By.ByCssSelector(value));
                    break;
                case "PARTIALLINK":
                    elementBys.put(name,new By.ByPartialLinkText(value));
                    break;
                case "LINK":
                    elementBys.put(name,new By.ByLinkText(value));
            }
        }
        return elementBys;
    }
}
